package org.laobai.controller;

import org.laobai.entity.Clazz;
import org.laobai.entity.User;
import org.laobai.struct.PageResult;
import org.laobai.struct.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("test")
public class TestController {
    private List<Clazz> clazzes = new ArrayList<>();
    private List<User> users = new ArrayList<>();

    static int i = 0;

    @PostConstruct
    public void init() {
        clazzes.add(new Clazz(1, "1901C"));
        clazzes.add(new Clazz(2, "1902C"));
        clazzes.add(new Clazz(3, "1901A"));
        clazzes.add(new Clazz(4, "1902A"));

        users.add(new User(1, "张三"));
        users.add(new User(2, "lisi"));
        users.add(new User(3, "马武"));
        users.add(new User(4, "赵柳"));
        users.add(new User(5, "钱琪"));
    }


    @GetMapping("getById")
    public Result<User> getUserById(Integer id) {
        if (id > 5) {
            return Result.error();
        }
        return Result.ok(users.get(id - 1));
    }

    @RequestMapping("page")
    public Result<PageResult<User>> getPage(int currPage, int pageSize, @RequestBody User user) throws InterruptedException {
        return Result.ok(PageResult.of(users, users.size()));
    }

    @PostMapping("save")
    public Result<String> save(@RequestBody User user) {
        return Result.ok("创建成功");
    }

    @PostMapping("save2")
    public Result<String> save2(User user) {
        return Result.ok("创建成功");
    }
}
